import BasePlugin from './BasePlugin';

/**
 * 选择插件 - 处理对象选择和操作
 */
export default class SelectionPlugin extends BasePlugin {
    constructor() {
        super();
        this.selectedObjects = [];
        this.isSelectionEnabled = true;
        this.selectionColor = 'rgba(0, 153, 255, 0.3)';
        this.cornerColor = '#0099ff';
        this.borderColor = '#0099ff';
        this.rotatingPointOffset = 40;
        this.padding = 5;
    }

    /**
     * 初始化插件
     */
    init(editor) {
        super.init(editor);
        this._setupSelectionStyle();
        this._setupEventListeners();
    }

    /**
     * 启用选择功能
     */
    enable() {
        super.enable();
        this.isSelectionEnabled = true;
        this.canvas.selection = true;
        this.canvas.renderAll();
    }

    /**
     * 禁用选择功能
     */
    disable() {
        super.disable();
        this.isSelectionEnabled = false;
        this.canvas.selection = false;
        this.canvas.discardActiveObject();
        this.canvas.renderAll();
    }

    /**
     * 选择所有对象
     */
    selectAll() {
        if (!this.isEnabled() || !this.canvas) return;

        const objects = this.canvas.getObjects();
        if (objects.length > 0) {
            const activeSelection = new fabric.ActiveSelection(objects, {
                canvas: this.canvas
            });
            this.canvas.setActiveObject(activeSelection);
            this.canvas.renderAll();
        }
    }

    /**
     * 取消选择所有对象
     */
    deselectAll() {
        if (!this.canvas) return;
        this.canvas.discardActiveObject();
        this.canvas.renderAll();
    }

    /**
     * 选择指定对象
     */
    selectObject(object) {
        if (!this.isEnabled() || !this.canvas || !object) return;
        this.canvas.setActiveObject(object);
        this.canvas.renderAll();
    }

    /**
     * 选择多个对象
     */
    selectObjects(objects) {
        if (!this.isEnabled() || !this.canvas || !objects.length) return;

        const activeSelection = new fabric.ActiveSelection(objects, {
            canvas: this.canvas
        });
        this.canvas.setActiveObject(activeSelection);
        this.canvas.renderAll();
    }

    /**
     * 获取当前选中的对象
     */
    getSelectedObjects() {
        if (!this.canvas) return [];

        const activeObject = this.canvas.getActiveObject();
        if (!activeObject) return [];

        return activeObject.type === 'activeSelection'
            ? activeObject.getObjects()
            : [activeObject];
    }

    /**
     * 锁定选中对象
     */
    lockSelection() {
        const objects = this.getSelectedObjects();
        objects.forEach(obj => {
            obj.lockMovementX = true;
            obj.lockMovementY = true;
            obj.lockRotation = true;
            obj.lockScalingX = true;
            obj.lockScalingY = true;
            obj.hasControls = false;
            obj.hasBorders = false;
            obj.selectable = false;
        });
        this.canvas.renderAll();
    }

    /**
     * 解锁选中对象
     */
    unlockSelection() {
        const objects = this.getSelectedObjects();
        objects.forEach(obj => {
            obj.lockMovementX = false;
            obj.lockMovementY = false;
            obj.lockRotation = false;
            obj.lockScalingX = false;
            obj.lockScalingY = false;
            obj.hasControls = true;
            obj.hasBorders = true;
            obj.selectable = true;
        });
        this.canvas.renderAll();
    }

    /**
     * 删除选中对象
     */
    deleteSelection() {
        const objects = this.getSelectedObjects();
        objects.forEach(obj => {
            this.canvas.remove(obj);
        });
        this.canvas.discardActiveObject();
        this.canvas.renderAll();
    }

    /**
     * 复制选中对象
     */
    duplicateSelection() {
        const objects = this.getSelectedObjects();
        if (!objects.length) return;

        const clonedObjects = [];
        const offset = 20;

        objects.forEach(obj => {
            obj.clone(cloned => {
                cloned.set({
                    left: cloned.left + offset,
                    top: cloned.top + offset
                });
                this.canvas.add(cloned);
                clonedObjects.push(cloned);

                if (clonedObjects.length === objects.length) {
                    const activeSelection = new fabric.ActiveSelection(clonedObjects, {
                        canvas: this.canvas
                    });
                    this.canvas.setActiveObject(activeSelection);
                    this.canvas.renderAll();
                }
            });
        });
    }

    /**
     * 设置选择样式
     */
    _setupSelectionStyle() {
        if (!this.canvas) return;

        this.canvas.set({
            selectionColor: this.selectionColor,
            selectionBorderColor: this.borderColor,
            selectionLineWidth: 1
        });

        // 设置控制点样式
        fabric.Object.prototype.set({
            cornerColor: this.cornerColor,
            cornerSize: 8,
            cornerStyle: 'circle',
            borderColor: this.borderColor,
            borderScaleFactor: 2,
            padding: this.padding,
            rotatingPointOffset: this.rotatingPointOffset
        });
    }

    /**
     * 设置事件监听
     */
    _setupEventListeners() {
        if (!this.canvas) return;

        // 监听选择变化事件
        this.canvas.on('selection:created', (e) => {
            if (!this.isEnabled()) return;
            this.selectedObjects = e.selected;
            this.editor.emit('selection:created', e.selected);
        });

        this.canvas.on('selection:updated', (e) => {
            if (!this.isEnabled()) return;
            this.selectedObjects = e.selected;
            this.editor.emit('selection:updated', e.selected);
        });

        this.canvas.on('selection:cleared', () => {
            if (!this.isEnabled()) return;
            this.selectedObjects = [];
            this.editor.emit('selection:cleared');
        });

        // 监听对象修改事件
        this.canvas.on('object:modified', (e) => {
            if (!this.isEnabled()) return;
            this.editor.emit('object:modified', e.target);
        });
    }

    /**
     * 销毁插件
     */
    destroy() {
        if (this.canvas) {
            this.deselectAll();
            this.selectedObjects = [];
        }
        super.destroy();
    }
} 